package com.bootdo.order.service.impl;

import static com.bootdo.common.utils.ShiroUtils.getUserId;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.merchant.dao.AgentDao;
import com.bootdo.merchant.dao.MerchantDao;
import com.bootdo.merchant.domain.AgentDO;
import com.bootdo.merchant.domain.MerchantDO;
import com.bootdo.order.dao.OrderDao;
import com.bootdo.order.dao.OrderPayDao;
import com.bootdo.order.domain.OrderDO;
import com.bootdo.order.domain.OrderPayDO;
import com.bootdo.order.service.OrderMercService;
import com.bootdo.order.utils.HttpClientUtil;
import com.bootdo.order.utils.MD5SignUtil;
import com.bootdo.system.dao.UserRoleDao;

/**
 * @Desc
 * @date 2019/12/2
 *
 */
@Service
public class OrderMercServiceImpl implements OrderMercService {

    @Autowired
    private OrderPayDao orderPayMapper;
    @Autowired
    private OrderDao orderMapper;
    @Autowired
    private MerchantDao merchantMapper;
    @Autowired
    private AgentDao agentMapper;
    @Autowired
    private UserRoleDao userRoleMapper;

    @Override
    public List<MerchantDO> findMerchant(Long userId){
        Map<String, Object> map = new HashMap<>();
        if (userId == null){
            //获取用户权限
            int checkUserIsManager = userRoleMapper.checkUserIsManager(getUserId());
            if (checkUserIsManager != 1){
                map.put("userIdCreate", getUserId());
            }
        } else {
            map.put("userIdCreate", userId);
        }
        return merchantMapper.list(map);
    }

    @Override
    public List<AgentDO> findAgent(){
        int checkUserIsManager = userRoleMapper.checkUserIsManager(getUserId());
        if (checkUserIsManager != 1){
            return null;
        }
        return agentMapper.list(new HashMap<>());
    }

    /**
     * 获取订单商户列表
     * @param map
     * @return
     */
    @Override
    public List<OrderPayDO> list(Map<String, Object> map){
        //格式话日期
        String startDate = (String) map.get("startTime");
        String endDate = (String) map.get("endTime");
        if (StringUtils.isNotBlank(startDate)){
            startDate = DateUtils.getDateOfFormat(startDate, DateUtils.DATE_TIME_PATTERN);
            endDate = DateUtils.getDateOfFormat(endDate, DateUtils.DATE_TIME_PATTERN);
            map.put("startTime", startDate);
            map.put("endTime", endDate);
        }
        int checkUserIsManager = userRoleMapper.checkUserIsManager(getUserId());
        if (checkUserIsManager != 1){
            map.put("userId", getUserId());
        }
        return orderPayMapper.mList(map);
    }

    /**
     * 获取条数
     * @param map
     * @return
     */
    @Override
    public int count(Map<String, Object> map){
        return orderPayMapper.mConut(map);
    }

    @Override
    public void callbackMerchant(Long id) throws Exception{
        OrderPayDO orderPay = orderPayMapper.get(id);
        MerchantDO merchant = merchantMapper.get(orderPay.getMerchantId());
        OrderDO order = orderMapper.get(orderPay.getOrderId());
        if (StringUtils.isNotBlank(order.getNotifyUrl())){
            Map<String, String> map = new HashMap<>();
            //支付类型
            map.put("payType", order.getPayType() + "");
            //实际支付金额
            map.put("actualMoney", order.getActualMoney() + "");
            //订单金额
            map.put("money", order.getMoney() + "");
            //平台订单号
            map.put("billNo", order.getId() + "");
            //商户自定义订单号
            map.put("orderNo", order.getOrderNo());
            map.put("identification", merchant.getIdentification());


            long requestTime = System.currentTimeMillis();
            map.put("requestTime", requestTime + "");
            Map<String, String> signMap = new HashMap<>();
            signMap.put("requestTime", requestTime + "");
            //商户token
            signMap.put("token", merchant.getToken());
            signMap.put("identification", merchant.getIdentification());
            signMap.put("payType", order.getPayType() + "");
            String sign = MD5SignUtil.sign(signMap);

            String str = HttpClientUtil.httpPost(order.getNotifyUrl(), map, sign);
            if (StringUtils.isBlank(str)){
                throw new Exception("回调失败");
            }
        }

    }
}
